/**
 * 日志存储
 * @author Shen
 */

const log4js = require('log4js');
// 定义级别:
const levels = {
    'trace': log4js.levels.TRACE,
    'debug': log4js.levels.DEBUG,
    'info': log4js.levels.INFO,
    'warn': log4js.levels.WARN,
    'error': log4js.levels.ERROR,
    'fatal': log4js.levels.FATAL
}

// 定义追加器配置:
log4js.configure({
    appenders: { 
        // 自定义追加器名字console, {type: 'console'}的意思是输出到console
        console: { type: 'console'}, 
        // 输出到文件，info & error
        info: {
            type: 'file',
            filename: 'logs/all-logs.log'
        },
        error: {
            type: 'dateFile',
            filename: 'logs/log',
            pattern: 'yyyy-MM-dd.log',
            alwaysIncludePattern: true
        }
    },
    categories: { // 默认情况下追加器种类是console,
        default: { appenders: ['console'], level: 'debug'},
        info: { 
            appenders: ['info','console'],
            level: 'info'
        },  
        error: { // 非默认状态下就是自定义名error
            // level级别设置为error
            appenders: ['error', 'console'],
            level: 'error'
        }
    }
})

/**
 * 日志输出，level为debug
 * @param {string} content 
 */
exports.debug = content => { // 实例，定级，输出
    const logger = log4js.getLogger('debug')
    logger.debug = levels.debug
    logger.debug(content)
}

exports.info = content => {
    const logger = log4js.getLogger('info')
    logger.info = levels.info
    logger.info(content)
}

exports.error = content => {
    const logger = log4js.getLogger('error')
    logger.error = levels.error
    logger.error(content)
}

